﻿using System;
using System.Collections.Generic;
using Microsoft.MediaCenter.UI;
using Microsoft.MediaCenter.Store;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.Hosting;
using System.Diagnostics;
using Microsoft.MediaCenter.Pvr;
using Microsoft.MediaCenter.Satellites;
using Microsoft.MediaCenter.Guide;
using System.IO;
using System.Xml;


namespace DVBS_HD_Wizard
{
    public class Model : ModelItem
    {

        EditableText logText = new EditableText();
        Microsoft.MediaCenter.TV.Epg.Lineup dummy;
        ArrayListDataSet dialogButtons = new ArrayListDataSet();

        public ObjectStore store;

        private bool firstrun; private bool iscomplete; private bool ischecked; private bool sat282; private bool sat192;
        private bool sat130; private bool isdiseqc;

        Timer timer1 = new Timer();
        public static AddInHost host;
        public static HistoryOrientedPageSession session;

        public Command _Finish; public Command _Dialog; public Command _Iscomplete; public Command _Search282;
        public Command _Search192; public Command _Search130; public Command _Tuner282;
        public Command _Tuner192; public Command _Tuner130; public Command _Close; public Command _back;

        private List<HDChannel> channels282;
        private List<HDChannel>.Enumerator currentChannel282;
        private List<HDChannel> channels192;
        private List<HDChannel>.Enumerator currentChannel192;
        private List<HDChannel> channels130;
        private List<HDChannel>.Enumerator currentChannel130;
        private HashSet<long> SatList = new HashSet<long>();

        private float progressStep282 = 0.1f; private float progressStep192 = 0.1f; private float progressStep130 = 0.1f;
        private float progress; private float save; 

        private BooleanChoice _checkBoxItem = new BooleanChoice();
        private BooleanChoice _checkBoxItem2 = new BooleanChoice();
        private BooleanChoice _checkBoxItem3 = new BooleanChoice();

        # region Initial

        public Model()
        {
            dummy = new Microsoft.MediaCenter.TV.Epg.Lineup();
            store = Util.DefaultObjectStore;
            logText.Value += string.Format("Object store {0} opened\r\n", store.StoreName);
            CheckSat();
            timer1.Tick +=new EventHandler(timer1_Tick);
            timer1.Interval = 1000;

            #region Astra 28.2

            channels282 = new List<HDChannel>(80); // Preallocate for 50 channels
            channels282.Add(new HDChannel(282, 10832, DvbsPolarization.LinearHorizontal, 22000, 2, 2049, 10000, 0, "ITV1 HD"));
            channels282.Add(new HDChannel(282, 11798, DvbsPolarization.LinearHorizontal, 29500, 2, 2005, 3875, 0, "Channel 4 HD"));
            channels282.Add(new HDChannel(282, 12266, DvbsPolarization.LinearHorizontal, 29500, 2, 2029, 3858, 0, "Five HD"));
            channels282.Add(new HDChannel(282, 12382, DvbsPolarization.LinearHorizontal, 29500, 2, 2035, 3880, 0, "Hallmark HD"));
            channels282.Add(new HDChannel(282, 11856, DvbsPolarization.LinearVertical, 29500, 2, 2008, 3876, 0, "E4 HD"));
            channels282.Add(new HDChannel(282, 11856, DvbsPolarization.LinearVertical, 29500, 2, 2008, 3823, 0, "FX HD"));
            channels282.Add(new HDChannel(282, 11856, DvbsPolarization.LinearVertical, 29500, 2, 2008, 3804, 0, "Eurosport HD"));
            channels282.Add(new HDChannel(282, 12012, DvbsPolarization.LinearVertical, 29500, 2, 2016, 3807, 0, "MGM HD"));
            channels282.Add(new HDChannel(282, 12012, DvbsPolarization.LinearVertical, 29500, 2, 2016, 3896, 0, "Sky Anytime HD1"));
            channels282.Add(new HDChannel(282, 11856, DvbsPolarization.LinearVertical, 29500, 2, 2008, 3864, 0, "Sky Arts 2 HD"));
            channels282.Add(new HDChannel(282, 12012, DvbsPolarization.LinearVertical, 29500, 2, 2016, 3818, 0, "Sky Movies Family HD"));
            channels282.Add(new HDChannel(282, 12012, DvbsPolarization.LinearVertical, 29500, 2, 2016, 3821, 0, "Sky Movies Premiere HD"));
            channels282.Add(new HDChannel(282, 12012, DvbsPolarization.LinearVertical, 29500, 2, 2016, 3862, 0, "Sky Movies Crime & Thriller HD"));
            channels282.Add(new HDChannel(282, 11798, DvbsPolarization.LinearHorizontal, 29500, 2, 2005, 3866, 0, ""));
            channels282.Add(new HDChannel(282, 11798, DvbsPolarization.LinearHorizontal, 29500, 2, 2005, 3802, 0, "Sky Sports HD1"));
            channels282.Add(new HDChannel(282, 11720, DvbsPolarization.LinearHorizontal, 29500, 2, 2001, 3806, 0, "MTVN HD"));
            channels282.Add(new HDChannel(282, 11720, DvbsPolarization.LinearHorizontal, 29500, 2, 2001, 3833, 0, "Sky Movies Showcase HD"));
            channels282.Add(new HDChannel(282, 11720, DvbsPolarization.LinearHorizontal, 29500, 2, 2001, 3881, 0, "Sky Sports HD2"));
            channels282.Add(new HDChannel(282, 12266, DvbsPolarization.LinearHorizontal, 29500, 2, 2029, 3857, 0, "ESPN America HD"));
            channels282.Add(new HDChannel(282, 12382, DvbsPolarization.LinearHorizontal, 29500, 2, 2035, 3820, 0, "Sky Movies Indie HD"));
            channels282.Add(new HDChannel(282, 12382, DvbsPolarization.LinearHorizontal, 29500, 2, 2035, 3898, 0, "Sky Anytime HD3"));
            channels282.Add(new HDChannel(282, 12363, DvbsPolarization.LinearVertical, 29500, 2, 2034, 3822, 0, "Sky Sports HD3"));
            channels282.Add(new HDChannel(282, 12363, DvbsPolarization.LinearVertical, 29500, 2, 2034, 3801, 0, "Sky Retail Info HD"));
            channels282.Add(new HDChannel(282, 12344, DvbsPolarization.LinearHorizontal, 29500, 2, 2033, 3861, 0, "Sky One HD"));
            channels282.Add(new HDChannel(282, 12344, DvbsPolarization.LinearHorizontal, 29500, 2, 2033, 3826, 0, "Sky Sports HD4"));
            channels282.Add(new HDChannel(282, 12090, DvbsPolarization.LinearVertical, 29500, 2, 2020, 3832, 0, "National Geographic HD"));
            channels282.Add(new HDChannel(282, 12324, DvbsPolarization.LinearVertical, 29500, 2, 2032, 3803, 0, "Discovery HD Showcase"));
            channels282.Add(new HDChannel(282, 12324, DvbsPolarization.LinearVertical, 29500, 2, 2032, 3873, 0, "Disney Cinemagic HD"));
            channels282.Add(new HDChannel(282, 12324, DvbsPolarization.LinearVertical, 29500, 2, 2032, 3842, 0, "ESPN UK HD"));
            channels282.Add(new HDChannel(282, 12324, DvbsPolarization.LinearVertical, 29500, 2, 2032, 3874, 0, "SyFy HD"));
            channels282.Add(new HDChannel(282, 12460, DvbsPolarization.LinearHorizontal, 29500, 2, 2039, 3850, 0, "Sky News HD"));
            channels282.Add(new HDChannel(282, 12460, DvbsPolarization.LinearHorizontal, 29500, 2, 2039, 3863, 0, "Sky Arts 1 HD"));
            channels282.Add(new HDChannel(282, 12460, DvbsPolarization.LinearHorizontal, 29500, 2, 2039, 3897, 0, "Sky Anytime HD2"));
            channels282.Add(new HDChannel(282, 12168, DvbsPolarization.LinearVertical, 29500, 2, 2024, 3815, 0, "Sky Modern Greats HD"));
            channels282.Add(new HDChannel(282, 12168, DvbsPolarization.LinearVertical, 29500, 2, 2024, 3816, 0, "Sky DraRom HD"));
            channels282.Add(new HDChannel(282, 12168, DvbsPolarization.LinearVertical, 29500, 2, 2024, 3817, 0, "Sky SciFi/Horror HD"));
            channels282.Add(new HDChannel(282, 12168, DvbsPolarization.LinearVertical, 29500, 2, 2024, 3819, 0, "Sky Comedy HD"));
            channels282.Add(new HDChannel(282, 12246, DvbsPolarization.LinearVertical, 29500, 2, 2028, 3814, 0, "Sky Action HD"));
            channels282.Add(new HDChannel(282, 12246, DvbsPolarization.LinearVertical, 29500, 2, 2028, 3885, 0, "Nat Geo Wild HD"));
            channels282.Add(new HDChannel(282, 12246, DvbsPolarization.LinearVertical, 29500, 2, 2028, 3886, 0, "History HD"));
            channels282.Add(new HDChannel(282, 12246, DvbsPolarization.LinearVertical, 29500, 2, 2028, 3887, 0, "Bio HD"));
            channels282.Add(new HDChannel(282, 12246, DvbsPolarization.LinearVertical, 29500, 2, 2028, 3888, 0, "Crime & Investigation Network HD"));
            channels282.Add(new HDChannel(282, 12090, DvbsPolarization.LinearVertical, 29500, 2, 2020, 3856, 0, "Comedy Central HD"));
            channels282.Add(new HDChannel(282, 12266, DvbsPolarization.LinearHorizontal, 29500, 2, 2029, 3849, 0, "Sky Sports News HD"));
            channels282.Add(new HDChannel(282, 10936, DvbsPolarization.LinearVertical, 22000, 2, 2056, 3851, 0, "ITV1 Granada HD"));
            channels282.Add(new HDChannel(282, 10936, DvbsPolarization.LinearVertical, 22000, 2, 2056, 3855, 0, "STV West HD"));
            channels282.Add(new HDChannel(282, 10936, DvbsPolarization.LinearVertical, 22000, 2, 2056, 3852, 0, "ITV Central West HD"));
            channels282.Add(new HDChannel(282, 12090, DvbsPolarization.LinearVertical, 29500, 2, 2020, 3812, 0, "Sky Movies Classics HD"));
            channels282.Add(new HDChannel(282, 12207, DvbsPolarization.LinearVertical, 27500, 2, 2026, 5107, 0, "Sky Three +1"));
         

            progressStep282 = 1.0f / channels282.Count;

            #endregion

            #region Astra 19.2

            channels192 = new List<HDChannel>(50); // Preallocate for 50 channels
            channels192.Add(new HDChannel(192, 10818, DvbsPolarization.LinearVertical, 22000, 1, 1056, 29953, 0, "Eurosport HD"));
            channels192.Add(new HDChannel(192, 10862, DvbsPolarization.LinearHorizontal, 22000, 1, 1059, 7120, 0, "TVP HD"));
            channels192.Add(new HDChannel(192, 11023, DvbsPolarization.LinearHorizontal, 22000, 133, 8, 126, 0, "Disney Cinemagic HD Germany"));
            channels192.Add(new HDChannel(192, 11023, DvbsPolarization.LinearHorizontal, 22000, 133, 8, 127, 0, "National Geographic HD Germany"));
            channels192.Add(new HDChannel(192, 11023, DvbsPolarization.LinearHorizontal, 22000, 133, 8, 128, 0, "History Channel HD Germany"));
            channels192.Add(new HDChannel(192, 11171, DvbsPolarization.LinearHorizontal, 22000, 1, 1047, 11007, 0, "Boing HD"));
            channels192.Add(new HDChannel(192, 11171, DvbsPolarization.LinearHorizontal, 22000, 1, 1047, 11009, 0, "KidsCo HD"));
            channels192.Add(new HDChannel(192, 11171, DvbsPolarization.LinearHorizontal, 22000, 1, 1047, 11031, 0, "Orange Ciné Max HD"));
            channels192.Add(new HDChannel(192, 11303, DvbsPolarization.LinearHorizontal, 22000, 1, 1007, 4911, 0, "Orf 1 HD"));
            channels192.Add(new HDChannel(192, 11303, DvbsPolarization.LinearHorizontal, 22000, 1, 1007, 4912, 0, "Orf 2 HD"));


            progressStep192 = 1.0f / channels192.Count;

            #endregion

            #region Hotbird 13.0

            channels130 = new List<HDChannel>(50); // Preallocate for 50 channels

            channels130.Add(new HDChannel(130, 11258, DvbsPolarization.LinearHorizontal, 27500, 318, 300, 15101, 0, "Wojna i Pokoj"));
            channels130.Add(new HDChannel(130, 11258, DvbsPolarization.LinearHorizontal, 27500, 318, 300, 15201, 0, "nFilm HD 2"));
            channels130.Add(new HDChannel(130, 11258, DvbsPolarization.LinearHorizontal, 27500, 318, 300, 15202, 0, "TVP HD"));
            channels130.Add(new HDChannel(130, 11258, DvbsPolarization.LinearHorizontal, 27500, 318, 300, 15203, 0, "Eurosport 2 HD"));
            channels130.Add(new HDChannel(130, 11258, DvbsPolarization.LinearHorizontal, 27500, 318, 300, 15204, 0, "nFilm HD"));
            channels130.Add(new HDChannel(130, 11258, DvbsPolarization.LinearHorizontal, 27500, 318, 300, 15205, 0, "NatGeo Wild HD"));
            channels130.Add(new HDChannel(130, 11258, DvbsPolarization.LinearHorizontal, 27500, 318, 300, 15206, 0, "Animal Planet HD"));
            channels130.Add(new HDChannel(130, 11258, DvbsPolarization.LinearHorizontal, 27500, 318, 300, 15207, 0, "Cinemax HD Eastern Europe"));


            progressStep130 = 1.0f / channels130.Count;

            #endregion

            #region UNUSED CheckboxItems

            //CheckBoxItem4.Description = "Hallmark HD";CheckBoxItem5.Description = "E4 HD";CheckBoxItem6.Description = "FX HD";

            //CheckBoxItem7.Description = "Eurosport HD";CheckBoxItem8.Description = "MGM HD";CheckBoxItem9.Description = "Sky Arts 2 HD";

            //CheckBoxItem10.Description = "Sky Movies Family HD";CheckBoxItem11.Description = "Sky Movies Premiere HD";CheckBoxItem12.Description = "Sky Movies Crime & Thriller HD";

            //CheckBoxItem13.Description = "Sky Anytime HD1";CheckBoxItem14.Description = "Sky Real Lives HD";CheckBoxItem15.Description = "Sky Sports HD1";

            //CheckBoxItem16.Description = "MTVN HD";CheckBoxItem17.Description = "Sky Movies Showcase HD";CheckBoxItem18.Description = "Sky Sports HD2";

            //CheckBoxItem19.Description = "ESPN America HD";CheckBoxItem20.Description = "Sky Movies Indie HD";CheckBoxItem21.Description = "Sky Anytime HD3";

            //CheckBoxItem22.Description = "Sky Sports HD3";CheckBoxItem23.Description = "Sky HD Retail Info";CheckBoxItem24.Description = "Sky 1 HD";

            //CheckBoxItem25.Description = "Sky Sports HD4";CheckBoxItem26.Description = "National Geographic Channel HD";CheckBoxItem27.Description = "Discovery HD Showcase";

            //CheckBoxItem28.Description = "Disney Cinemagic HD";CheckBoxItem29.Description = "ESPN UK HD";CheckBoxItem30.Description = "Syfy HD";

            //CheckBoxItem31.Description = "Sky News HD";CheckBoxItem32.Description = "Sky Arts 1 HD";CheckBoxItem33.Description = "Sky Anytime HD 2";

            //CheckBoxItem34.Description = "Sky Movies Modern Greats HD";CheckBoxItem35.Description = "Sky Movies Drama & Romance HD";CheckBoxItem36.Description = "Sky Movies Sci-Fi & Horror HD";

            //CheckBoxItem37.Description = "Sky Movies Comedy HD";CheckBoxItem38.Description = "Sky Movies Action & Adventure HD";CheckBoxItem39.Description = "Nat Geo Wild HD";

            //CheckBoxItem40.Description = "History HD";CheckBoxItem41.Description = "Bio HD";CheckBoxItem42.Description = "Crime & Investigation Network HD";

            //CheckBoxItem43.Description = "Comedy Central HD"; CheckBoxItem44.Description = "Sky Sports News HD"; CheckBoxItem45.Description = "ITV1 Granada HD";

            #endregion

            CheckBoxItem3.Description = "Hotbird 6-8-9 (13.0°E)"; CheckBoxItem.Description = "Astra 2A-B-D at (28.2°E) Eurobird (28.5°E)";
            CheckBoxItem2.Description = "Astra 1E-1F-1G-1H-1KR-2C at (19.2°E) ";

            _Close = new Command(this, "My Button2", new EventHandler(Close_Invoked));
            _Tuner282 = new Command(this, "My Button", new EventHandler(Tuner282_Invoked));
            _Tuner192 = new Command(this, "My Button8", new EventHandler(Tuner192_Invoked));
            _Tuner130 = new Command(this, "My Button11", new EventHandler(Tuner130_Invoked));
            _back = new Command(this, "My Button3", new EventHandler(Back_Invoked));
            _Finish = new Command(this, "My Button4", new EventHandler(Finish_Invoked));
            _Search282 = new Command(this, "My Button5", new EventHandler(search282_Invoked));
            _Search192 = new Command(this, "My Button9", new EventHandler(search192_Invoked));
            _Search130 = new Command(this, "My Button10", new EventHandler(search130_Invoked));
            _Dialog = new Command(this, "My Button6", new EventHandler(Dialog_Invoked));
            _Iscomplete = new Command(this, "My Button7", new EventHandler(Iscomplete_Invoked));

        }

        # endregion

        # region Commands

        #region UNUSED BooleanChoice

        //private BooleanChoice _checkBoxItem4 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem5 = new BooleanChoice();private BooleanChoice _checkBoxItem6 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem7 = new BooleanChoice();private BooleanChoice _checkBoxItem8 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem9 = new BooleanChoice();private BooleanChoice _checkBoxItem10 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem11 = new BooleanChoice();private BooleanChoice _checkBoxItem12 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem13 = new BooleanChoice();private BooleanChoice _checkBoxItem14 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem15 = new BooleanChoice();private BooleanChoice _checkBoxItem16 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem17 = new BooleanChoice();private BooleanChoice _checkBoxItem18 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem19 = new BooleanChoice();private BooleanChoice _checkBoxItem20 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem21 = new BooleanChoice();private BooleanChoice _checkBoxItem22 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem23 = new BooleanChoice();private BooleanChoice _checkBoxItem24 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem25 = new BooleanChoice();private BooleanChoice _checkBoxItem26 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem27 = new BooleanChoice();private BooleanChoice _checkBoxItem28 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem29 = new BooleanChoice();private BooleanChoice _checkBoxItem30 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem31 = new BooleanChoice();private BooleanChoice _checkBoxItem32 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem33 = new BooleanChoice();private BooleanChoice _checkBoxItem34 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem35 = new BooleanChoice();private BooleanChoice _checkBoxItem36 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem37 = new BooleanChoice();private BooleanChoice _checkBoxItem38 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem39 = new BooleanChoice();private BooleanChoice _checkBoxItem40 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem41 = new BooleanChoice();private BooleanChoice _checkBoxItem42 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem43 = new BooleanChoice(); private BooleanChoice _checkBoxItem44 = new BooleanChoice();
        //private BooleanChoice _checkBoxItem45 = new BooleanChoice();

        #endregion

        public Command Tuner282 { get { return _Tuner282; } }
        public Command Tuner192 { get { return _Tuner192; } }
        public Command Tuner130 { get { return _Tuner130; } }
        public Command Close { get { return _Close; } }
        public Command Back { get { return _back; } }
        public Command Finish { get { return _Finish; } }
        public Command Search282 { get { return _Search282; } }
        public Command Search192 { get { return _Search192; } }
        public Command Search130 { get { return _Search130; } }
        public Command Dialog { get { return _Dialog; } }
        public Command Iscomplete { get { return _Iscomplete; } }

        # endregion

        # region History

        public Model(HistoryOrientedPageSession session, AddInHost host)
        {
            Model.session = session;
            Model.host = host;
        }

        # endregion

        # region MCE Environment

        public MediaCenterEnvironment mediaCenterEnvironment
        {
            get
            {
                if (host == null) return null;
                return host.MediaCenterEnvironment;
            }
        }

        # endregion

        # region Bool

        public bool IsDiseqc
        {
            get { return isdiseqc; }
            set
            {
                if (isdiseqc != value)
                {
                    isdiseqc = value;
                    FirePropertyChanged("IsDiseqc");
                }
            }
        }

        public bool Sat282
        {
            get { return sat282; }
            set
            {
                if (sat282 != value)
                {
                    sat282 = value;
                    FirePropertyChanged("Sat282");
                }
            }
        }

        public bool Sat192
        {
            get { return sat192; }
            set
            {
                if (sat192 != value)
                {
                    sat192 = value;
                    FirePropertyChanged("Sat192");
                }
            }
        }

        public bool Sat130
        {
            get { return sat130; }
            set
            {
                if (sat130 != value)
                {
                    sat130 = value;
                    FirePropertyChanged("Sat130");
                }
            }
        }

        public bool IsComplete
        {
            get { return iscomplete; }
            set
            {
                if (iscomplete != value)
                {
                    iscomplete = value;
                    FirePropertyChanged("IsComplete");
                }
            }
        }

        public bool Ischecked
        {
            get { return ischecked; }
            set
            {
                if (ischecked != value)
                {
                    ischecked = value;
                    FirePropertyChanged("Ischecked");
                }
            }
        }

        public bool FirstRun
        {
            get { return firstrun; }
            set
            {
                if (firstrun != value)
                {
                    firstrun = value;
                    FirePropertyChanged("FirstRun");
                }
            }
        }


        #endregion

        #region BooleanChoice

        public BooleanChoice CheckBoxItem
        {
            get
            {
                if (_checkBoxItem == null)
                {
                    _checkBoxItem = new BooleanChoice();
                }
                return _checkBoxItem;
            }
            set { _checkBoxItem = value; }
        }

        public BooleanChoice CheckBoxItem2
        {
            get
            {
                if (_checkBoxItem2 == null)
                {
                    _checkBoxItem2 = new BooleanChoice();
                }
                return _checkBoxItem2;
            }
            set { _checkBoxItem2 = value; }
        }

        public BooleanChoice CheckBoxItem3
        {
            get
            {
                if (_checkBoxItem3 == null)
                {
                    _checkBoxItem3 = new BooleanChoice();
                }
                return _checkBoxItem3;
            }
            set { _checkBoxItem3 = value; }
        }

        #endregion

        #region UNUSED Checkbox Bindings

        // public BooleanChoice CheckBoxItem4
        //{
        //  get
        //{
        //  if (_checkBoxItem4 == null)
        //{
        //  _checkBoxItem4 = new BooleanChoice();
        //}
        //return _checkBoxItem4;
        //}
        //set { _checkBoxItem4 = value; }
        //}

        // public BooleanChoice CheckBoxItem5
        //{
        //  get
        //{
        //  if (_checkBoxItem5 == null)
        //{
        //  _checkBoxItem5 = new BooleanChoice();
        //}
        //return _checkBoxItem5;
        //}
        //set { _checkBoxItem5 = value; }
        //}

        // public BooleanChoice CheckBoxItem6
        //{
        //  get
        //{
        //  if (_checkBoxItem6 == null)
        //{
        //  _checkBoxItem6 = new BooleanChoice();
        //}
        //return _checkBoxItem6;
        //}
        //set { _checkBoxItem6 = value; }
        //}

        //  public BooleanChoice CheckBoxItem7
        //{
        //  get
        //{
        //  if (_checkBoxItem7 == null)
        //{
        //  _checkBoxItem7 = new BooleanChoice();
        //}
        //return _checkBoxItem7;
        //}
        //set { _checkBoxItem7 = value; }
        //}

        // public BooleanChoice CheckBoxItem8
        //{
        //  get
        //{
        //  if (_checkBoxItem8 == null)
        //{
        //  _checkBoxItem8 = new BooleanChoice();
        //}
        //return _checkBoxItem8;
        //}
        //set { _checkBoxItem8 = value; }
        //}

        // public BooleanChoice CheckBoxItem9
        //{
        //  get
        //{
        //  if (_checkBoxItem9 == null)
        //{
        //  _checkBoxItem9 = new BooleanChoice();
        //}
        //return _checkBoxItem9;
        //}
        //set { _checkBoxItem9 = value; }
        //}

        // public BooleanChoice CheckBoxItem10
        //{
        //  get
        //{
        //  if (_checkBoxItem10 == null)
        //{
        //  _checkBoxItem10 = new BooleanChoice();
        //}
        //return _checkBoxItem10;
        //}
        //set { _checkBoxItem10 = value; }
        //}

        //  public BooleanChoice CheckBoxItem11
        //{
        //  get
        //{
        //  if (_checkBoxItem11 == null)
        //{
        //   _checkBoxItem11 = new BooleanChoice();
        //}
        //return _checkBoxItem11;
        //}
        //set { _checkBoxItem11 = value; }
        //}

        // public BooleanChoice CheckBoxItem12
        //{
        //  get
        //{
        //  if (_checkBoxItem12 == null)
        //{
        //  _checkBoxItem12 = new BooleanChoice();
        //}
        //return _checkBoxItem12;
        //}
        //set { _checkBoxItem12 = value; }
        //}

        // public BooleanChoice CheckBoxItem13
        //{
        //  get
        //{
        //  if (_checkBoxItem13 == null)
        //{
        //  _checkBoxItem13 = new BooleanChoice();
        //}
        //return _checkBoxItem13;
        //}
        //set { _checkBoxItem13 = value; }
        //}

        // public BooleanChoice CheckBoxItem14
        //{
        //  get
        //{
        //  if (_checkBoxItem14 == null)
        //{
        //  _checkBoxItem14 = new BooleanChoice();
        //}
        //return _checkBoxItem14;
        //}
        //set { _checkBoxItem14 = value; }
        //}

        // public BooleanChoice CheckBoxItem15
        //{
        //  get
        //{
        //  if (_checkBoxItem15 == null)
        //{
        //  _checkBoxItem15 = new BooleanChoice();
        //}
        //return _checkBoxItem15;
        //}
        //set { _checkBoxItem15 = value; }
        //}

        // public BooleanChoice CheckBoxItem16
        //{
        //  get
        //{
        //  if (_checkBoxItem16 == null)
        //{
        //  _checkBoxItem16 = new BooleanChoice();
        //}
        //return _checkBoxItem16;
        //}
        //set { _checkBoxItem16 = value; }
        //}

        // public BooleanChoice CheckBoxItem17
        //{
        //  get
        //{
        //  if (_checkBoxItem17 == null)
        //{
        //  _checkBoxItem17 = new BooleanChoice();
        //}
        //return _checkBoxItem17;
        //}
        //set { _checkBoxItem17 = value; }
        //}

        // public BooleanChoice CheckBoxItem18
        //{
        //  get
        //{
        //  if (_checkBoxItem18 == null)
        //{
        //  _checkBoxItem18 = new BooleanChoice();
        //}
        //return _checkBoxItem18;
        //}
        //set { _checkBoxItem18 = value; }
        //}

        // public BooleanChoice CheckBoxItem19
        //{
        //  get
        //{
        //  if (_checkBoxItem19 == null)
        //{
        //  _checkBoxItem19 = new BooleanChoice();
        //}
        //return _checkBoxItem19;
        //}
        //set { _checkBoxItem19 = value; }
        //}

        /// public BooleanChoice CheckBoxItem20
        //{
        //  get
        //{
        //  if (_checkBoxItem20 == null)
        //{
        //  _checkBoxItem20 = new BooleanChoice();
        //}
        //return _checkBoxItem20;
        //}
        //set { _checkBoxItem20 = value; }
        //}

        // public BooleanChoice CheckBoxItem21
        //{
        //  get
        //{
        //  if (_checkBoxItem21 == null)
        //{
        //  _checkBoxItem21 = new BooleanChoice();
        //}
        //return _checkBoxItem21;
        //}
        //set { _checkBoxItem21 = value; }
        //}

        //public BooleanChoice CheckBoxItem22
        //{
        //  get
        //{
        //  if (_checkBoxItem22 == null)
        //{
        //  _checkBoxItem22 = new BooleanChoice();
        //}
        //return _checkBoxItem22;
        //}
        //set { _checkBoxItem22 = value; }
        //}

        //public BooleanChoice CheckBoxItem23
        //{
        //  get
        //{
        //  if (_checkBoxItem23 == null)
        //{
        //  _checkBoxItem23 = new BooleanChoice();
        //}
        //return _checkBoxItem23;
        //}
        //set { _checkBoxItem23 = value; }
        //}

        //public BooleanChoice CheckBoxItem24
        //{
        //  get
        //{
        //  if (_checkBoxItem24 == null)
        //{
        //  _checkBoxItem24 = new BooleanChoice();
        //}
        //return _checkBoxItem24;
        //}
        //set { _checkBoxItem24 = value; }
        //}

        // public BooleanChoice CheckBoxItem25
        //{
        //  get
        //{
        //  if (_checkBoxItem25 == null)
        //{
        //  _checkBoxItem25 = new BooleanChoice();
        //}
        //return _checkBoxItem25;
        //}
        //set { _checkBoxItem25 = value; }
        //}

        // public BooleanChoice CheckBoxItem26
        //{
        //  get
        //{
        //  if (_checkBoxItem26 == null)
        //{
        //  _checkBoxItem26 = new BooleanChoice();
        //}
        //return _checkBoxItem26;
        //}
        //set { _checkBoxItem26 = value; }
        //}

        // public BooleanChoice CheckBoxItem27
        //{
        //  get
        //{
        //  if (_checkBoxItem27 == null)
        //{
        //  _checkBoxItem27 = new BooleanChoice();
        //}
        //return _checkBoxItem27;
        //}
        //set { _checkBoxItem27 = value; }
        //}

        //public BooleanChoice CheckBoxItem28
        //{
        //  get
        //{
        //  if (_checkBoxItem28 == null)
        //{
        //  _checkBoxItem28 = new BooleanChoice();
        //}
        //return _checkBoxItem28;
        //}
        //set { _checkBoxItem28 = value; }
        //}

        // public BooleanChoice CheckBoxItem29
        //{
        //  get
        //{
        //  if (_checkBoxItem29 == null)
        //{
        //  _checkBoxItem29 = new BooleanChoice();
        //}
        //return _checkBoxItem29;
        //}
        //set { _checkBoxItem29 = value; }
        //}

        // public BooleanChoice CheckBoxItem30
        //{
        //  get
        //{
        //  if (_checkBoxItem30 == null)
        //{
        //  _checkBoxItem30 = new BooleanChoice();
        //}
        //return _checkBoxItem30;
        //}
        //set { _checkBoxItem30 = value; }
        //}

        //  public BooleanChoice CheckBoxItem31
        //{
        //  get
        //{
        //  if (_checkBoxItem31 == null)
        //{
        //  _checkBoxItem31 = new BooleanChoice();
        //}
        //return _checkBoxItem31;
        //}
        //set { _checkBoxItem31 = value; }
        //}

        // public BooleanChoice CheckBoxItem32
        //{
        //  get
        //{
        //  if (_checkBoxItem32 == null)
        //{
        //  _checkBoxItem32 = new BooleanChoice();
        //}
        //return _checkBoxItem32;
        //}
        //set { _checkBoxItem32 = value; }
        //}

        // public BooleanChoice CheckBoxItem33
        //{
        //  get
        //{
        //  if (_checkBoxItem33 == null)
        //{
        //  _checkBoxItem33 = new BooleanChoice();
        //}
        //return _checkBoxItem33;
        //}
        //set { _checkBoxItem33 = value; }
        //}

        // public BooleanChoice CheckBoxItem34
        //{
        //  get
        //{
        //  if (_checkBoxItem34 == null)
        //{
        //  _checkBoxItem34 = new BooleanChoice();
        //}
        //return _checkBoxItem34;
        //}
        //set { _checkBoxItem34 = value; }
        //}

        // public BooleanChoice CheckBoxItem35
        //{
        //  get
        //{
        //  if (_checkBoxItem35 == null)
        //{
        //  _checkBoxItem35 = new BooleanChoice();
        //}
        //return _checkBoxItem35;
        //}
        //set { _checkBoxItem35 = value; }
        //}

        // public BooleanChoice CheckBoxItem36
        //{
        //  get
        //{
        //  if (_checkBoxItem36 == null)
        //{
        //  _checkBoxItem36 = new BooleanChoice();
        //}
        //return _checkBoxItem36;
        //}
        //set { _checkBoxItem36 = value; }
        //}

        // public BooleanChoice CheckBoxItem37
        //{
        //  get
        //{
        //  if (_checkBoxItem37 == null)
        //{
        //  _checkBoxItem37 = new BooleanChoice();
        //}
        //return _checkBoxItem37;
        //}
        //set { _checkBoxItem37 = value; }
        //}

        // public BooleanChoice CheckBoxItem38
        //{
        //  get
        //{
        //  if (_checkBoxItem38 == null)
        //{
        //  _checkBoxItem38 = new BooleanChoice();
        //}
        //return _checkBoxItem38;
        //}
        //set { _checkBoxItem38 = value; }
        //}

        // public BooleanChoice CheckBoxItem39
        //{
        //  get
        //{
        //  if (_checkBoxItem39 == null)
        //{
        //   _checkBoxItem39 = new BooleanChoice();
        //}
        //return _checkBoxItem39;
        //}
        //set { _checkBoxItem39 = value; }
        //}

        //public BooleanChoice CheckBoxItem40
        //{
        //  get
        //{
        //  if (_checkBoxItem40 == null)
        //{
        //   _checkBoxItem40 = new BooleanChoice();
        //}
        //return _checkBoxItem40;
        //}
        //set { _checkBoxItem40 = value; }
        //}

        // public BooleanChoice CheckBoxItem41
        //{
        //  get
        //{
        //  if (_checkBoxItem41 == null)
        //{
        //  _checkBoxItem41 = new BooleanChoice();
        //}
        //return _checkBoxItem41;
        //}
        //set { _checkBoxItem41 = value; }
        //}

        //public BooleanChoice CheckBoxItem42
        //{
        //  get
        //{
        //  if (_checkBoxItem42 == null)
        //{
        //   _checkBoxItem42 = new BooleanChoice();
        //}
        //return _checkBoxItem42;
        //}
        //set { _checkBoxItem42 = value; }
        //}

        //public BooleanChoice CheckBoxItem43
        //{
        // get
        //{
        //  if (_checkBoxItem43 == null)
        //{
        //  _checkBoxItem43 = new BooleanChoice();
        //}
        //return _checkBoxItem43;
        //}
        // set { _checkBoxItem43 = value; }
        //}

        //public BooleanChoice CheckBoxItem44
        //{
        //get
        //{
        //if (_checkBoxItem44 == null)
        //{
        // _checkBoxItem44 = new BooleanChoice();
        //}
        //return _checkBoxItem44;
        //}
        //set { _checkBoxItem44 = value; }
        //}

        //public BooleanChoice CheckBoxItem45
        //{
        //get
        //{
        //if (_checkBoxItem45 == null)
        //{
        // _checkBoxItem45 = new BooleanChoice();
        //}
        //return _checkBoxItem45;
        //}
        //set { _checkBoxItem45 = value; }//
        //}

        #endregion

        # region Invokers

        public void GoToMenu()
        {
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Model"] = this;

            if (session != null)
            {
                session.GoToPage("resx://DVBS_HD_Wizard/DVBS_HD_Wizard.Resources/Default", properties);
            }

        }

        public void Dialog_Invoked(object sender, EventArgs e)
        {
            dialogButtons.Add("OK");
            dialogButtons.Add("Cancel");
            String dialogText = "Are you sure you want to cancel & exit";
            String dialogTitle = "Cancel Update?";
            mediaCenterEnvironment.Dialog(dialogText, dialogTitle, dialogButtons, 10, true, null,
            new Microsoft.MediaCenter.DialogClosedCallback(versionCallback));

        }

        public void versionCallback(Microsoft.MediaCenter.DialogResult result)
        {
            if (result.ToString() == "100")
            {
                session.Close();
                ObjectStore.DisposeAll();
            }
        }

        public void Close_Invoked(object sender, EventArgs e)
        {
            session.Close();
            ObjectStore.DisposeAll();
        }

        public void Finish_Invoked(object sender, EventArgs e)
        {
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Model"] = this;

            session.GoToPage("resx://DVBS_HD_Wizard/DVBS_HD_Wizard.Resources/SavingSettings", properties);
        }

        public void search282_Invoked(object sender, EventArgs e)
        {
            FirstRun = true;
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Model"] = this;

            session.GoToPage("resx://DVBS_HD_Wizard/DVBS_HD_Wizard.Resources/ChannelSearch282", properties);
        }

        public void search192_Invoked(object sender, EventArgs e)
        {
            FirstRun = true;
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Model"] = this;

            session.GoToPage("resx://DVBS_HD_Wizard/DVBS_HD_Wizard.Resources/ChannelSearch192", properties);
        }

        public void search130_Invoked(object sender, EventArgs e)
        {
            FirstRun = true;
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Model"] = this;

            session.GoToPage("resx://DVBS_HD_Wizard/DVBS_HD_Wizard.Resources/ChannelSearch130", properties);
        }


        public void Tuner282_Invoked(object sender, EventArgs e)
        {
            currentChannel282 = channels282.GetEnumerator();
            TimeSpan ts = new TimeSpan(0, 0, 0, 0, 1);
            if (currentChannel282.MoveNext())
                Application.DeferredInvoke(delegate { ProcessChannel282(); }, ts);
        }

        public void Tuner192_Invoked(object sender, EventArgs e)
        {
            currentChannel192 = channels192.GetEnumerator();
            TimeSpan ts = new TimeSpan(0, 0, 0, 0, 1);
            if (currentChannel192.MoveNext())
                Application.DeferredInvoke(delegate { ProcessChannel192(); }, ts);
        }

        public void Tuner130_Invoked(object sender, EventArgs e)
        {
            currentChannel130 = channels130.GetEnumerator();
            TimeSpan ts = new TimeSpan(0, 0, 0, 0, 1);
            if (currentChannel130.MoveNext())
                Application.DeferredInvoke(delegate { ProcessChannel130(); }, ts);
        }

        public void Back_Invoked(object sender, EventArgs e)
        {
            session.BackPage();
        }

        public void Iscomplete_Invoked(object sender, EventArgs e)
        {
            timer1.Start();
            StreamWriter log = new StreamWriter
                ("C:\\Program Files\\ACS Digital Media\\DVBS HD Wizard\\Log.txt");       
            log.WriteLine(logText.Value);    
            log.Close(); 

            TimeSpan tsp = new TimeSpan(0, 0, 0, 10);
            Application.DeferredInvoke(
               delegate { savesettings(); }, tsp);
        }

        public void savesettings()
        {
            timer1.Stop();
            session.Close();
            ObjectStore.DisposeAll();
        }

        # endregion

        #region ProgressBar

        public void timer1_Tick(object sender, EventArgs e)
        {
            SaveProgress += 0.1f;
        }

        public float DownloadProgress
        {
            get { return progress; }
            set
            {
                if (value < 0)
                {
                    value = 0;
                }

                if (value > 1.0f)
                {
                    value = 1.0f;
                }

                if (progress != value)
                {
                    progress = value;
                    FirePropertyChanged("DownloadProgress");
                }
            }
        }

        public float SaveProgress
        {
            get { return save; }
            set
            {
                if (value < 0)
                {
                    value = 0;
                }

                if (value > 1.0f)
                {
                    value = 1.0f;
                }

                if (save != value)
                {
                    save = value;
                    FirePropertyChanged("SaveProgress");
                }
            }
        }

        #endregion

        #region Switches

        private void ProcessChannel282()
        {
            DownloadProgress += progressStep282;

            switch (currentChannel282.Current.PreChecks(logText, store))
            {
                case HDChannel.CheckResult.Needed:
                    //CheckBoxItem.Chosen = true;
                    Ischecked = true;
                    break;

                case HDChannel.CheckResult.Failed:
                    mediaCenterEnvironment.Dialog
                       ("Search failed", "Channel Search", DialogButtons.Ok, 0, true);

                    break;

                case HDChannel.CheckResult.NotNeeded:
                    //CheckBoxItem.Chosen = false;
                    Ischecked = false;
                    break;

                default:
                  
                    break;
            }


            TimeSpan ts = new TimeSpan(0, 0, 0, 0,80);
            if (currentChannel282.MoveNext())
                Application.DeferredInvoke(delegate { ProcessChannel282(); }, ts);
            else
            {
                Dictionary<string, object> properties = new Dictionary<string, object>();
                properties["Model"] = this;

                session.GoToPage("resx://DVBS_HD_Wizard/DVBS_HD_Wizard.Resources/Completed", properties);
            }
        }

        private void ProcessChannel192()
        {
            DownloadProgress += progressStep192;

            switch (currentChannel192.Current.PreChecks(logText, store))
            {
                case HDChannel.CheckResult.Needed:
                    //CheckBoxItem.Chosen = true;
                    Ischecked = true;
                    break;

                case HDChannel.CheckResult.Failed:
                    mediaCenterEnvironment.Dialog
                       ("Search failed", "Channel Search", DialogButtons.Ok, 0, true);

                    break;

                case HDChannel.CheckResult.NotNeeded:
                    //CheckBoxItem.Chosen = false;
                    Ischecked = false;
                    break;

                default:
                   
                    break;
            }


            TimeSpan ts = new TimeSpan(0, 0, 0, 0, 80);
            if (currentChannel192.MoveNext())
                Application.DeferredInvoke(delegate { ProcessChannel192(); }, ts);
            else
            {
                Sat192 = false;
                Dictionary<string, object> properties = new Dictionary<string, object>();
                properties["Model"] = this;

                session.GoToPage("resx://DVBS_HD_Wizard/DVBS_HD_Wizard.Resources/Completed", properties);
            }
        }

        private void ProcessChannel130()
        {
            DownloadProgress += progressStep130;

            switch (currentChannel130.Current.PreChecks(logText, store))
            {
                case HDChannel.CheckResult.Needed:
                    //CheckBoxItem.Chosen = true;
                    Ischecked = true;
                    break;

                case HDChannel.CheckResult.Failed:
                    mediaCenterEnvironment.Dialog
                       ("Search failed", "Channel Search", DialogButtons.Ok, 0, true);

                    break;

                case HDChannel.CheckResult.NotNeeded:
                    //CheckBoxItem.Chosen = false;
                    Ischecked = false;
                    break;

                default:
                  
                    break;
            }


            TimeSpan ts = new TimeSpan(0, 0, 0, 0, 80);
            if (currentChannel130.MoveNext())
                Application.DeferredInvoke(delegate { ProcessChannel130(); }, ts);
            else
            {
                Sat130 = false;
                Dictionary<string, object> properties = new Dictionary<string, object>();
                properties["Model"] = this;

                session.GoToPage("resx://DVBS_HD_Wizard/DVBS_HD_Wizard.Resources/Completed", properties);
            }
        }

        #endregion

        #region Satellite Check

        public void CheckSat()
        {
            logText.Value += "Performing Pre-Checks\r\n";

            logText.Value += "Finding enabled devices\r\n";
            foreach (DeviceGroup group in new DeviceGroups(store))
            {
                if (group.IsEnabled && group.ContainsTunerType(TunerType.DvbS))
                {
                    // Enabled DVB-S devices

                    logText.Value += "Looking for devices\r\n";
                    foreach (Device dev in group.Devices)
                    {
                        if (dev.DeviceType.TuningSpaceName == "DVB-S")
                        {
                            logText.Value += String.Format("* {0}\r\n", dev.Name);
                            if (dev.LowNoiseBlocks == null)
                            {
                                logText.Value += String.Format(" # No LNBs - Skipping\r\n");
                                continue;
                            }

                            foreach (LowNoiseBlock lnb in dev.LowNoiseBlocks)
                            {
                                logText.Value += String.Format(" * {0}\r\n", lnb.Satellite.PositionEast);

                                SatList.Add(lnb.Satellite.PositionEast);

                                if (SatList.Count > 1)
                                {
                                    IsDiseqc = true;
                                }

                                foreach (long position in SatList)
                                {
                                    switch (position)
                                    {
                                        case 282:
                                            Sat282 = true;
                                            CheckBoxItem.Chosen = true;
                                            SatList.Add(lnb.Satellite.PositionEast);
                                            break;
                                        case 192:
                                            Sat192 = true;
                                            CheckBoxItem2.Chosen = true;
                                            SatList.Add(lnb.Satellite.PositionEast);
                                            break;
                                        case 130:
                                            Sat130 = true;
                                            CheckBoxItem3.Chosen = true;
                                            SatList.Add(lnb.Satellite.PositionEast);
                                            break;
                                        default:
                                            logText.Value += String.Format(" - {0} Ignored\r\n", lnb.Satellite.PositionEast);
                                            break;
                                    }

                                }
                            }
                        }
                    }
                }

            }

        }
          #endregion
    }
}

